﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySoft.Data;

namespace Caifu.Datas
{
	/// <summary>
	/// 每日净值
	/// </summary>
	public class NetValueDailyData
	{
		/// <summary>
		/// 
		/// </summary>
		public static readonly NetValueDailyData Instance = new NetValueDailyData();

		private readonly DbSession dbSession = new DbSession("Caifu");

		/// <summary>
		/// 
		/// </summary>
		/// <param name="netValueDaily"></param>
		public void AddNetValueDaily(NetValueDaily netValueDaily)
		{
			netValueDaily.Detach();
			if (netValueDaily.Id == Guid.Empty) netValueDaily.Id = Itfort.Comb.NewComb();
			if (netValueDaily.AddTime == DateTime.MinValue) netValueDaily.AddTime = DateTime.Now;
			if (netValueDaily.UpdateTime == DateTime.MinValue) netValueDaily.UpdateTime = DateTime.Now;
			dbSession.Save<NetValueDaily>(netValueDaily);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="netValueDaily"></param>
		public void UpdateNetValueDaily(NetValueDaily netValueDaily)
		{
			netValueDaily.Attach();
			netValueDaily.UpdateTime = DateTime.Now;
			dbSession.Save<NetValueDaily>(netValueDaily);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fundId"></param>
		public void DeleteNetValueDaily(Guid fundId)
		{
			var where = NetValueDaily._.FundId == fundId;
			dbSession.Delete<NetValueDaily>(where);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fundId"></param>
		/// <param name="reportDate"></param>
		/// <returns></returns>
		public NetValueDaily GetNetValueDaily(Guid fundId)
		{
			var where = NetValueDaily._.FundId == fundId;
			return dbSession.From<NetValueDaily>().Where(where).ToSingle();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public IList<NetValueDaily> GetNetValueDailys(Int32 pageIndex, Int32 pageSize)
		{
			return dbSession.From<NetValueDaily>()
				.OrderBy(NetValueDaily._.ReportDate.Desc)
				.OrderBy(NetValueDaily._.GrowthRate.Desc)
				.GetPage(pageSize).ToList(pageIndex);
		}
	}
}
